//
//  HomeModel.swift
//  AgricultureApp
//
//  Created by eport_user1 on 2018/1/10.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

import UIKit

/*
 {
 "id": 1946,
 "title": "热烈祝贺夏邑厂部通过省专家组清洁生产验收",
 "manufacturer": {
 "id": 11324,
 "level": "FREE",
 "full_name": "河南郑氏化工产品有限公司",
 "short_name": "",
 "certify_status": "UNCERTIFIED"
 },
 "summary": "热烈祝贺郑氏化工夏邑厂部通过省专家组清洁生产验收",
 "entity_type": "MANUFACTURER_NEWS",
 "view_count": 0,
 "comment_count": 0,
 "publish_time": "2018-01-10T23:37:13.000+08:00",
 "pic_url": "http://cdn.n369.com/Fu3JwmRQNjNp0bmu5hdjaTkZGaNT?imageView2/2/w/1440",
 "thumb_url": "http://cdn.n369.com/Fu3JwmRQNjNp0bmu5hdjaTkZGaNT?imageView2/1/w/240/h/144",
 "content_thumbs": ["http://cdn.n369.com/Fke5XwufZISZuP5G6MulSI6iJ1E-?imageView2/1/w/240/h/144", "http://cdn.n369.com/Fu3JwmRQNjNp0bmu5hdjaTkZGaNT?imageView2/1/w/240/h/144"]
 }
 */

//枚举 同样默认其他类可以访问
enum ModelType : Int {
    case product = 1
    case productNews = 2
    case productCases = 3
    case productVideo = 4
    case farmNews = 5
    case other = 6
}


enum CellType : Int {
    case noPic = 1
    case onePic = 2
    case threePic = 3
    case other = 4
}


class HomeModel: NSObject {
    
    var newsId:Int?
    var title:String?
    var factModel:Manufacturer?
    var summary:String?
    var entity_type:String?
    var view_count:Int?
    var comment_count:Int?
    var publish_time:String?
    var pic_url:String?
    var thumb_url:String?
    var content_thumbs:[String]?
    var type:ModelType?
    var cellType:CellType?
    
    override init() {
        
        print("初始化HomeModel模型")
    }
    
    init(dict:[String:Any]) {
        
       newsId = dict["id"] as? Int ?? 0
       title = dict["title"] as? String ?? ""
       summary = dict["summary"] as? String ?? ""
       entity_type = dict["entity_type"] as? String ?? ""
        view_count = dict["view_count"] as? Int ?? 0
        comment_count = dict["comment_count"] as? Int ?? 0
        publish_time = dict["publish_time"] as? String ?? ""
        pic_url = dict["pic_url"] as? String ?? ""
        thumb_url = dict["thumb_url"] as? String ?? ""
        content_thumbs = dict["content_thumbs"] as? [String] ?? [""]
        
        let dic = dict["manufacturer"] as? [String:Any] ?? ["":""]
        let model1 = Manufacturer(dict: dic)
        factModel = model1
        
        type = HomeModel.getModelType(typeStr: entity_type!)
        cellType = HomeModel.getCellPic(images: content_thumbs!)
    }
    
   class func getCellPic(images:[String]) -> CellType  {
        
        var type:CellType?
       
        if images.count > 2{
            
            type = CellType.threePic
            
        }else if images.count > 0{
            
            type = CellType.onePic
            
        }
        else{
            type = CellType.noPic
            
        }
    
        return type!
        
    }

    
   class func getModelType(typeStr:String) -> ModelType {
        
        var type:ModelType?
        
        if (typeStr == "PRODUCT") {
            type = ModelType.product
        } else if (typeStr == "MANUFACTURER_NEWS") {
            type = ModelType.productNews
        } else if (typeStr == "PRODUCT_CASE") {
            type = ModelType.productCases
        } else if (typeStr == "PRODUCT_VIDEO") {
            type = ModelType.productVideo
        } else if (typeStr == "PORTAL_NEWS") {
            type = ModelType.farmNews
        } else {
            type = ModelType.other
        }
        
        return type!
    }
    
}

class Manufacturer: NSObject {
    
   /*
     "manufacturer": {
     "id": 11324,
     "level": "FREE",
     "full_name": "河南郑氏化工产品有限公司",
     "short_name": "",
     "certify_status": "UNCERTIFIED"
     },
     */
    
    var factID:Int?
    var level:String?
    var full_name:String?
    var short_name:String?
    var certify_status:String?
    
    init(dict:[String:Any]){
        
        factID = Int((dict["id"] as? NSString ?? "0").intValue)
        
        level = dict["level"] as? String ?? ""
        full_name = dict["full_name"] as? String ?? ""
        short_name = dict["short_name"] as? String ?? ""
        certify_status = dict["certify_status"] as? String ?? ""
    }
    
}


